package org.zjx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zjx.api.entity.Room;
import org.zjx.common.route.IRoute;
import org.zjx.common.route.RouteManager;
import org.zjx.common.route.bean.Address;
import org.zjx.common.route.bean.Coordinate;
import org.zjx.common.route.routes.RunRoute;
import org.zjx.common.route.routes.TransferApertureRoute;
import org.zjx.common.route.routes.TransferNPCRoute;
import org.zjx.dao.entity.Screen;
import org.zjx.dao.entity.Waypoint;
import org.zjx.dao.mapper.RoomMapper;
import org.zjx.dao.mapper.WaypointMapper;
import org.zjx.dao.vo.WayPointsVO;
import org.zjx.service.IWaypointService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-29
 */
@Service
public class WaypointServiceImpl extends ServiceImpl<WaypointMapper, Waypoint> implements IWaypointService {

    @Autowired
    private RoomServiceImpl roomService;

    @Autowired
    private ScreenServiceImpl screenService;

    @Override
    public List<IRoute> routes(List<IRoute> routes, Address start, Address end) {

        RouteManager routeManager = new RouteManager(start);
        if (routes != null) routeManager.addRoutes(routes);

        List<IRoute> newWayPoint = new ArrayList<>();
        List<Waypoint> waypoints = this.list();
        for (Waypoint waypoint : waypoints) {
            Address sAddress = new Address(waypoint.getStartNote(), waypoint.getStartPointX(), waypoint.getStartPointY());
            Address eAddress = new Address(waypoint.getEndNote(), waypoint.getEndPointX(), waypoint.getEndPointY());
            if (waypoint.getType().startsWith("光圈")) {
                TransferApertureRoute route = new TransferApertureRoute(sAddress,eAddress);
                route.setClickPoint(new Coordinate(waypoint.getApertureX(), waypoint.getApertureY()));
                newWayPoint.add(route);
            }else if (waypoint.getType().startsWith("房间")) {
                TransferApertureRoute route = new TransferApertureRoute(sAddress,eAddress);
                route.setToRoom(true);
                route.setClickPoint(new Coordinate(waypoint.getApertureX(), waypoint.getApertureY()));
                newWayPoint.add(route);
            }else if (waypoint.getType().startsWith("NPC")) {
                TransferNPCRoute route = new TransferNPCRoute(sAddress,eAddress);

                Screen screen = screenService.selectByScreenName(sAddress.getCity());
                route.setScreenSize(new Coordinate(screen.getSizeX(),screen.getSizeY()));

                route.setNpcName(waypoint.getType());
                String ss = waypoint.getDialogues();
                if (ss != null && !ss.isEmpty()){
                    String[] dialogues = ss.split(";");
                    route.setDialogues(List.of(dialogues));
                }
                route.setNpcAddress(new Address(waypoint.getStartNote(), waypoint.getApertureX(), waypoint.getApertureY()));
                newWayPoint.add(route);
            }
        }
        routeManager.addWayPointRoutes(newWayPoint);

        List<IRoute> path = routeManager.findPath(end);
        if (path == null) return null;

        List<Room> rooms = roomService.list();

        for (IRoute route : path) {
            if (route instanceof RunRoute) {
                for (Room room : rooms) {
                    if (room.getName().equals(route.getStart().getCity())) {
                        ((RunRoute) route).setRoom(true);
                        break;
                    }
                }
            }
        }


        System.out.printf("从 %s 到 %s 的最短路径：%n", start, end);
        path.forEach(System.out::println);
        System.out.printf("预计总耗时：%d秒%n%n", path.stream().mapToInt(IRoute::getTime).sum());


        return path;
    }
}
